<template>
  <div class="system-log-container">
    <div class="spaceForm">
      <el-form />
      <el-form>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
      </el-form>
    </div>

    <el-table v-loading="listLoading" :data="list">
      <el-table-column align="center" label="任务ID" prop="id" width="80px" />
      <el-table-column
        align="center"
        label="任务标题"
        prop="title"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="任务接口"
        prop="fire_params"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="接口参数"
        prop="api_params"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="执行间隔"
        prop="crontab_interval_time"
        width="80"
      />
      <el-table-column
        align="center"
        label="最后执行时间"
        prop="last_fire_time"
        width="200"
      >
        <template #default="{ row }">
          <div v-html="resetTime(row.last_fire_time)"></div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="启动状态"
        prop="enable"
        width="100"
      >
        <template #default="{ row }">
          <div v-if="row.enable == 1">启动</div>
          <div v-else>禁止</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="进程状态" prop="state" width="100">
        <template #default="{ row }">
          <div v-if="row.state == 1">运行中</div>
          <div v-else-if="row.state == 0">空闲</div>
          <div v-else-if="row.state == -1">异常退出</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="85"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">详情</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <task-detail ref="taskDetail" @fetch-data="fetchData" />
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
import { getTask, deleteTask } from '@/api/task'
import { tenBitTimestamp } from '@/utils/index'
import taskDetail from './components/detail'
export default {
  name: 'Notice',
  components: { taskDetail },
  data() {
    return {
      list: [],
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      queryForm: {
        page: 1,
        perpage: 20,
      },
      typeList: [],
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleSizeChange(val) {
      this.queryForm.perpage = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.fetchData()
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm('你确定要删除当前计划任务吗', null, async () => {
          const data = await deleteTask({ id: row.id })
          if (data.is_success) {
            this.$baseMessage('删除成功', 'success', 'vab-hey-message-success')
            await this.fetchData()
          }
        })
      } else {
        this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
      }
    },
    handleEdit(row) {
      if (row.id) {
        this.$refs['taskDetail'].showEdit(row)
      } else {
        this.$refs['taskDetail'].showEdit()
      }
    },
    queryData() {
      this.queryForm.page = 1
      this.fetchData()
    },
    async fetchData() {
      this.listLoading = true
      const {
        data: { items, total },
      } = await getTask(this.queryForm)
      this.list = items
      this.total = total
      this.listLoading = false
    },
    resetTime(time) {
      var date = tenBitTimestamp(time)
      return date
    },
  },
}
</script>
